package com.xsm.ppsecret.util;

import android.content.Context;
import android.content.SharedPreferences;

/**
 * Created by xsm on 17-2-9.
 */

public class ShareManager {
    public static final String NAME = "ppsecret";
    public static final String REGIST = "regist";
    public static final String FIRST_USE_CHAT = "first_use_chat";
    public static final String USERNAME = "username";

    public static final String PASSWORD_ROOT = "password_root";
    public static final String PASSWORD_ONE = "password_1";
    public static final String PASSWORD_TWO = "password_2";
    public static final String ENCRYPTED_ANSWER = "encrypted_answer";
    public static final String ENCRYPTED_QUESTION = "encrypted_question";

    private static ShareManager mannger;
    private Context context;
    private ShareManager(Context cxt) {
        context = cxt;
    }

    public static synchronized void init(Context cxt){
        if(mannger == null){
            mannger = new ShareManager(cxt);
        }
    }

    public synchronized static ShareManager getInstance() {
        if (mannger == null) {
            throw new RuntimeException("please init preferenceManager first!");
        }
        return mannger;
    }

    public boolean isRegist() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(REGIST, false);
    }
    public void setRegistTrue() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(REGIST, true);
        editor.apply();
    }

    public boolean isFirstUseChat() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getBoolean(FIRST_USE_CHAT, true);
    }
    public void setNotFirstUseChat() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_APPEND);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putBoolean(FIRST_USE_CHAT, false);
        editor.apply();
    }

    public void setUsername(String username) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(USERNAME, username);
        editor.apply();
    }
    public String getUsername() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(USERNAME, "");
    }

    public void setPasswordRoot(String password) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(PASSWORD_ROOT, password);
        editor.apply();
    }
    public String getPassword() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(PASSWORD_ROOT, "");
    }

    public void setPasswordOne(String passwordOne) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(PASSWORD_ONE, passwordOne);
        editor.apply();
    }
    public String getPasswordOne() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(PASSWORD_ONE, "");
    }

    public void setPasswordTwo(String passwordTwo) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(PASSWORD_TWO, passwordTwo);
        editor.apply();
    }
    public String getPasswordTwo() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(PASSWORD_TWO, "");
    }

    public void setEncryptedQuestion(String question) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(ENCRYPTED_QUESTION, question);
        editor.apply();
    }
    public String getEncryptedQuestion() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(ENCRYPTED_QUESTION, "");
    }

    public void setEncryptedAnswer(String answer) {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(ENCRYPTED_ANSWER, answer);
        editor.apply();
    }
    public String getEncryptedAnswer() {
        SharedPreferences preferences = context.getSharedPreferences(NAME, Context.MODE_PRIVATE);
        return preferences.getString(ENCRYPTED_ANSWER, "");
    }
}
